package com.htfyun.ft.fragment.factory;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.LruCache;

import com.htfyun.ft.FTApplication;
import com.htfyun.ft.R;
import com.htfyun.ft.config.FTBuildConfig;
import com.htfyun.ft.config.NVRomKeysAttr;
import com.htfyun.ft.device.DeviceAttr;
import com.htfyun.ft.utils.DebugLog;
import com.htfyun.hardware.NVRomController;

import androidx.fragment.app.Fragment;


public enum FactoryTestItemsEnum {

    main(null, -1, FragmentMain.class),

    screen(NVRomKeysAttr.screen, R.string.ft_item_screen, FragmentScreen.class),

    led(NVRomKeysAttr.led_breath, R.string.ft_item_led, FragmentLed.class),

    tp(NVRomKeysAttr.finger_tp, R.string.ft_item_finger_tp, FragmentTP.class),

    speaker(NVRomKeysAttr.speaker, R.string.ft_item_speaker, FragmentSpeaker.class),

    camera(NVRomKeysAttr.camera, R.string.ft_item_camera, FragmentCamera.class),

    keys(NVRomKeysAttr.keys, R.string.ft_item_key, FragmentKey.class),
    keys_android(NVRomKeysAttr.keys_android, R.string.ft_item_key, FragmentKeyAndroid.class),

    mic_2_channel(NVRomKeysAttr.mic_2_channel, R.string.ft_item_mic, FragmentMic2Channel.class),
    mic_6_channel(NVRomKeysAttr.mic_6_channel, R.string.ft_item_mic, FragmentMic6Channel.class),
    unisound_mic_test(NVRomKeysAttr.unisound_mic_test, R.string.ft_item_unisound_mic_test, FragmentUnisoundMicTest.class),

    bluetooth(NVRomKeysAttr.bluetooth, R.string.ft_item_bluetooth, FragmentBluetooth.class),

    wifi(NVRomKeysAttr.wifi, R.string.ft_item_wifi, FragmentWifi.class),

    sdcard(NVRomKeysAttr.sdcard, R.string.ft_item_sdcard, FragmentSDCard.class),

    light_sensor(NVRomKeysAttr.light_sensor, R.string.ft_item_light_sensor, FragmentLightSensor.class),

    proximity_sensor(NVRomKeysAttr.proximity_sensor, R.string.ft_item_proximity_sensor, FragmentProximitySensor.class),

    accelerate_sensor(NVRomKeysAttr.accelerate_sensor, R.string.ft_item_accelerate_sensor, FragmentAccelerateSensor.class),

    hall_sensor(NVRomKeysAttr.hall_sensor, R.string.ft_item_hall_sensor, FragmentHallSensor.class),

    battery(NVRomKeysAttr.battery, R.string.ft_item_battery_chg, FragmentBattery.class),

    ram_rom(NVRomKeysAttr.ram_rom, R.string.ft_item_ram_rom, FragmentRamRom.class),

    servo(NVRomKeysAttr.servo, R.string.ft_item_servo,FragmentServo_bk.class),

    led_rgb(NVRomKeysAttr.led_rgb, R.string.ft_item_led_rgb, FragmentLedRGB.class),

    type_c(NVRomKeysAttr.type_c, R.string.ft_item_type_c, FragmentTypeC.class),

//    wakeup( NVRomKeysAttr.wakeup,
//            R.string.ft_item_wakeup, R.string.ft_item_wakeup_en, null),

    ;
    private final NVRomKeysAttr nvromKey;
    private final int nameResId;
    private final Class<? extends Fragment> itemFragment;

    private FactoryTestItemsEnum(NVRomKeysAttr nvromKey,
                                 int nameResId, Class<? extends Fragment> fragment) {
        this.nvromKey = nvromKey;
        this.nameResId = nameResId;
        itemFragment = fragment;
    }

    public NVRomKeysAttr getNVRomKey() {

        if (!DeviceAttr.isNVRomKeySupported(nvromKey)) {
            return null;
        }

        return nvromKey;
    }

    public String newNVRomKeyForAfterAging(String key) {
        return key + "_afterAging";
    }
    public String newNVRomKeyForLog(String key) {
        return key + "_log";
    }

    private int getNameResId() {
        return nameResId;
    }

    public String getName() {
        if (getNameResId() < 0) {
            return "";
        }
        try {
            final Context context = FTApplication.getContext();
            return context.getString(getNameResId());
        } catch (Exception e) {

        }
        return "";

    }

    public Class<? extends Fragment> getItemFragment() {
        return itemFragment;
    }


    //////////////////////////////////////////
    //////////////////////////////////////////


    public ResultAttr getResult() {

        NVRomKeysAttr key = getNVRomKey();
        if (key == null) {
            return ResultAttr.PASS;
        }

        return doGetResult(key.getKey());
    }

    public void setResult(ResultAttr result) {

        NVRomKeysAttr key = getNVRomKey();
        if (key == null) {
            return;
        }

        doSetResult(key.getKey(), result);
    }


    public Drawable getDrawable() {

        final Context context = FTApplication.getContext();

        ResultAttr result = getResult();

        switch (result) {

            case PASS:
                return context.getDrawable(R.drawable.selector_background_pass);
            case FAIL:
                return context.getDrawable(R.drawable.selector_background_fail);
            case MA:
                default:
                    return context.getDrawable(R.drawable.selector_background_ma);
        }
    }

    public String getResultForLog() {

        NVRomKeysAttr key = getNVRomKey();
        if (key == null) {
            return "";
        }

        return nvromGetString(newNVRomKeyForLog(key.getKey()));

    }

    public void setResultForLog(String log) {

        NVRomKeysAttr key = getNVRomKey();
        if (key == null) {
            return;
        }

        nvromSetString(newNVRomKeyForLog(key.getKey()), log);
    }

    private final static String SHARED_PREFERENCES_NAME = "ft_factory_test_items";

    private ResultAttr doGetResult(String key) {

        if (FTBuildConfig.SAVE_TEST_RESULT_INTO_NVROM) {

           return ResultAttr.toResultAttr(nvromGetString(key));

        } else {

            final Context context = FTApplication.getContext();

            ResultAttr defResult = ResultAttr.MA;

            SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);

            return ResultAttr.toResultAttr(sp.getString(key, defResult.getResult()));
        }

    }

    private void doSetResult(String key, ResultAttr result) {

        DebugLog.e("doSetResult key = " + key + ", result = " + result);

        if (FTBuildConfig.SAVE_TEST_RESULT_INTO_NVROM) {

            ResultAttr savedResult = ResultAttr.toResultAttr(nvromGetString(key));
            if (savedResult != result) {
                nvromSetString(key, result.getResult());
            }

        } else {

            final Context context = FTApplication.getContext();

            SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();

            editor.putString(key, result.getResult());
            editor.apply();
        }

    }

    private static LruCache<String, String> nvromCache = new LruCache<>(NVRomKeysAttr.values().length);

    public String nvromGetString(String key) {
        if (TextUtils.isEmpty(key)) {
            return "";
        }
        String value = nvromCache.get(key);
        if (value == null) {
            value = NVRomController.getInstance().getNVRomString(key);
            if (TextUtils.isEmpty(value)) {
                value = "";
            }
            nvromCache.put(key, value);
        }

        return value;
    }

    public void nvromSetString(String key, String value) {
        nvromCache.put(key, value);
        NVRomController.getInstance().setNVRomString(key, value);
    }


    public boolean isPassed() {

        return isPassedWithResult(getResult());
    }

    private boolean isPassedWithResult(ResultAttr result) {
        return result == ResultAttr.PASS;
    }

    public static FactoryTestItemsEnum getItemEnumByFragment(final Fragment fragment) {
        for (FactoryTestItemsEnum itemsEnum : values()) {
            if (itemsEnum.getItemFragment() != null && itemsEnum.getItemFragment().isInstance(fragment)) {
                return itemsEnum;
            }
        }
        return null;
    }

}
